package com.omega.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.omega.constants.ApplicationConstants;
import com.omega.dao.RetailerDao;
import com.omega.entity.Retailer;
import com.omega.service.RetailerService;

@Service
public class RetailerServiceImpl implements RetailerService {

	@Autowired
	RetailerDao retailerDao;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addRetailer(Retailer retailer) {
		retailer.setretailerActive(ApplicationConstants.ACTIVE);
		retailerDao.addRetailer(retailer);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public List<Retailer> loadAllRetailers() {
		return retailerDao.loadAllRetailers();
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Retailer loadById(Integer supplierId) {
		return retailerDao.loadById(supplierId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateRetailer(Retailer retailer) {
		
		Retailer retailerEntity = retailerDao.loadById(retailer.getRetailerId());
		retailerEntity.setTinNumber(retailer.getTinNumber());
		retailerEntity.setMonth(retailer.getMonth());
		retailerEntity.setYear(retailer.getYear());
		retailerEntity.setretailerAddress(retailer.getretailerAddress());
		retailerEntity.setretailerEmailId(retailer.getretailerEmailId());
		retailerEntity.setretailerMobileNumber(retailer.getretailerMobileNumber());
		retailerEntity.setretailerName(retailer.getretailerName());
		retailerEntity.setretailerPhoneNumber(retailer.getretailerPhoneNumber());
		retailerDao.updateRetailer(retailerEntity);
		
	}

	@Override
	public void activateRetailer(Integer retailerId) {
		Retailer retailerEntity = retailerDao.loadById(retailerId);
		retailerEntity.setretailerActive(ApplicationConstants.SUPPLIER_ACTIVE);
		retailerDao.updateRetailer(retailerEntity);
	}

	@Override
	public void deactivateRetailer(Integer retailerId) {
		Retailer retailerEntity = retailerDao.loadById(retailerId);
		retailerEntity.setretailerActive(ApplicationConstants.SUPPLIER_INACTIVE);
		retailerDao.updateRetailer(retailerEntity);
	}	

}
